package com.xb.loan.cashier.biz.task;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.xb.loan.cashier.biz.constant.FundConst;
import com.xb.loan.cashier.biz.dao.bean.CashierEncashOrder;
import com.xb.loan.cashier.biz.task.bean.ThirdAccountBean;
import com.xb.loan.cashier.biz.task.bean.ThirdAccountEncashBean;
import com.xb.loan.cashier.service.constant.ICashierEnumConst;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 与第三方提现对账任务
 *
 */
public abstract class ThirdAccountEncashTask extends ThirdAccountTask {
    private static final Logger logger = LoggerFactory.getLogger(ThirdAccountEncashTask.class);

    protected static final int orderType = FundConst.FundType_Encash;       // 订单类型为提现

    
    // 更新第三方账期时间
    protected void updateThirdAccountTimeBatch(List<String> orderIds){
        cashierEncashOrderService.updateThirdAccountTime(orderIds, day);
    }
    
    protected String getStartTime(String day) {
        return getStartTime(day, null);
    }
    
    protected String getEndTime(String day){
        return getEndTime(day, null);
    }
    
    // 边界的处理(包含开始)
    protected String getStartTime(String day, String pattern) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            pattern = StringUtils.isBlank(pattern)?"yyyy-MM-dd HH:mm:ss":pattern;
            SimpleDateFormat sdf1 = new SimpleDateFormat(pattern);
            return sdf1.format(sdf.parse(day));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    // 边界的处理(不包含结束)
    protected String getEndTime(String day, String pattern) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            pattern = StringUtils.isBlank(pattern)?"yyyy-MM-dd HH:mm:ss":pattern;
            SimpleDateFormat sdf1 = new SimpleDateFormat(pattern);
            return sdf1.format(DateUtils.addDays(sdf.parse(day), 1));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    
    @Override
    protected int getOrderType() {
        return orderType;
    }
    
    
    protected List<ThirdAccountBean> getThirdLessDatas(String day, String accountId, int pageNo, int pageSize){
        return convert(cashierEncashOrderService.selectThirdLessData(day, accountId, pageNo++, PAGE_SIZE));
    }
    
    @Override
    protected List<ThirdAccountBean> getAccountNonDatas(String day, String accountId, int pageNo, int pageSize) {
        return convert(cashierEncashOrderService.selectAccountNonDatas(day, accountId, pageNo, PAGE_SIZE));
    }
    
    protected List<ThirdAccountBean> getDBAccountDatasByIds(List<String> orderIds) {
        return convert(cashierEncashOrderService.selectByOrderIds(orderIds));
    }
    
    protected List<ThirdAccountBean> convert(List<CashierEncashOrder> list){
        if(CollectionUtils.isEmpty(list)){
            return null;
        }
        List<ThirdAccountBean> result = new ArrayList<ThirdAccountBean>();
        for(CashierEncashOrder order : list){
            result.add(convert(order, day));
        }
        return result;
    }

    protected ThirdAccountBean convert(CashierEncashOrder order, String accountDayThird) {
        ThirdAccountEncashBean result = new ThirdAccountEncashBean();
        result.setAccountDay(order.getAccountDayPlat());
        result.setAccountDayThird(accountDayThird);
        result.setMoney(order.getMoney());
        result.setOrderId(order.getOrderId());
        result.setOrderIdThird(order.getChannelOrderId());
        result.setStatus(convertStatus(order.getStatus()));
        result.setSellAccount(order.getSellAccount());
        return result;
    }

    protected int convertStatus(int status) {
        if(status== ICashierEnumConst.CashierEncashStatus.WAIT.getStatus()
                || status== ICashierEnumConst.CashierEncashStatus.WAITING.getStatus()){
            return ThirdAccountBean.STATUS_WAIT;
        }else if(status== ICashierEnumConst.CashierEncashStatus.SUCCESS.getStatus()){
            return ThirdAccountBean.STATUS_SUCCESS;
        }else if(status== ICashierEnumConst.CashierEncashStatus.FAIL.getStatus()){
            return ThirdAccountBean.STATUS_FAIL;
        }
        logger.warn("convertStatus msg=状态转换异常 status="+status);
        throw new RuntimeException("convertStatus msg=状态转换异常 status="+status);
    }
    
    public int convertStatusToDBStatus(int beanStatus){
        if(beanStatus==ThirdAccountBean.STATUS_SUCCESS){
            return ICashierEnumConst.CashierEncashStatus.SUCCESS.getStatus();
        }else if(beanStatus==ThirdAccountBean.STATUS_FAIL){
            return ICashierEnumConst.CashierEncashStatus.FAIL.getStatus();
        }
        logger.warn("convertStatus msg=状态转换异常 status="+beanStatus);
        throw new RuntimeException("convertStatusToDBStatus msg=状态转换异常 beanStatus="+beanStatus);
    }
    
    @Override
    protected boolean compare(ThirdAccountBean dbBean, ThirdAccountBean thirdBean, boolean compareStatas) {
        ThirdAccountEncashBean dbData = (ThirdAccountEncashBean)dbBean;
        ThirdAccountEncashBean thirdData = (ThirdAccountEncashBean)thirdBean;
        if(dbData==null && thirdData==null){
            return true;
        }
        if(dbData==null){
            return false;
        }
        if(thirdData==null){
            return false;
        }
        if(!dbData.getOrderId().equals(thirdData.getOrderId())){
            return false;
        }
        if(dbData.getMoney()!=thirdData.getMoney()){
            return false;
        }
        if(compareStatas){
            if(dbData.getStatus()!=thirdData.getStatus()){
                return false;
            }
        }
        if(!dbData.getSellAccount().equals(thirdData.getSellAccount())){
            return false;
        }
        return true;
    }
    
    @Override
    protected int updateResult(String orderId, int beanStatus) {
        int row = 0;
        try {
            int status = convertStatusToDBStatus(beanStatus);
            row = cashierEncashOrderService.updateEncashResult1(orderId, status, accountDay);
//            // TODO 推送可以放到异步执行
//            cashierEncashOrderService.pushEncashOrder(orderId);
        } catch (Exception e) {
            logger.error("msg=更新提现结果异常 orderId="+orderId, e);
        }
        return row;
    }
    
    @Override
    protected boolean updateAccountStatus(String accountId, String orderId, int accountStatus, String day) {
        int row = cashierEncashOrderService.updateAccountStatus(accountId, orderId, accountStatus, day);
        return row>0;
    }
    
}
